package com.podhradie.net.themperature.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

import com.podhradie.net.themperature.tools.Context;
import com.podhradie.net.themperature.tools.Context.LogCategory;

public class UdpServer  {
	private DatagramChannel udpChannel;
    private ByteBuffer buffer;
	private String ipAddress;
	private int port;
	private boolean run;
    
    public UdpServer (String ipAddress, int port){
    	this.ipAddress = ipAddress;
    	this.port = port;
    	run = true;
		Thread t1 = new Thread(thServer);
		t1.start();
    }
    
    public void stop (){
       	run = false;
    	udpChannel.socket().close();
        Context.get().printLine(LogCategory.UDP_SERVER,"Stop listening on socket " +ipAddress+":"+port);
        Context.get().setUdpListening(false);
    }

    Runnable thServer = new Runnable() {
    public void run() {
        try {
            udpChannel = DatagramChannel.open(); //otev��t UDP kan�l
            udpChannel.socket().bind(new InetSocketAddress(ipAddress, port)); //za��t naslouchat
            buffer = ByteBuffer.allocateDirect(512); //alokovat buffer
            Context.get().printLine(LogCategory.UDP_SERVER,"Start listening on socket " +ipAddress+":"+port);
            Context.get().setUdpListening(true);
            while(run) {
                SocketAddress client = udpChannel.receive(buffer); //p�ijmout UDP paket
                Context.get().printLine(LogCategory.UDP_SERVER,"Connection on socket " +client);
                buffer.clear(); //vymazat obsah bufferu
                buffer.put(createLine().getBytes()); 
                buffer.flip();
                udpChannel.send(buffer, client); //odeslat zp�t
            }
        }
        catch(IOException e) {
           // e.printStackTrace(System.err);
            Context.get().printLine(LogCategory.UDP_SERVER,"Clossing server");
            Context.get().setUdpListening(false);
        }
        finally {
            try {
                if(udpChannel != null) udpChannel.close();
            }
            catch(IOException e) {
            	e.printStackTrace();
            }
        }
        Context.get().setUdpListening(false);
    }
    };
	public String createLine (){
		if (Context.get().getaTemperature()==-1000){
			return "aTemperature:"+"N/A"+";"+"timestamp:"+"N/A";
		}
		return "aTemperature:"+String.valueOf(Context.get().getaTemperature())+";"+"timestamp:"+Context.get().getTimeStamp().toString();
	}
	/**
	 * @return the ipAddress
	 */
	public String getIpAddress() {
		return ipAddress;
	}
	/**
	 * @param ipAddress the ipAddress to set
	 */
	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}
	/**
	 * @param port the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

}
